<?php
class Pager extends Config
{
	var $num = 7; //news on page
	var $start = 2; //start pagination
	var $page = int;
	var $allFields  = int;
	var $tableName = string;
	var $uniQuery = string;
	var $dbQuery = string;
	
	
	function page_calculate($tableInput = "_hosts", $queryInput = null)
	{
		//Detect variables
		$this -> tableName = $this -> DB_PREFIX . $tableInput;
		$this -> uniQuery = $queryInput;
		$this -> page = intval($_GET['page']);
		
		//Get count
		$query1 = "SELECT COUNT(*) FROM `". $this -> tableName ."` ". $this -> uniQuery .";";
		
		//Calculate
		$sql1 = $this -> sql($query1);
		$arr = mysql_fetch_array($sql1);
		$this -> allFields = $arr['COUNT(*)'];
		$this -> pages = intval(($this -> allFields -1) / $this -> num ) +1;
		if ( empty($this -> page) || $this -> page <= 0 )
			$this -> page = 1;
		if ( $this -> page > $this -> pages )
			$this -> page = $this -> pages;
		$this -> start = intval( $this -> page * $this -> num - $this -> num );
	}
	
	
	function page_list()
	{
		$query1 = "SELECT * FROM `". $this -> tableName ."` ". $this -> uniQuery ." LIMIT ". $this -> start .", ". $this -> num ." ;";
		//$this -> dbQuery = $this -> sql($query1);
		return $this -> sql($query1);
	}
	
	function page_list_data()
	{
		$query1 = $this -> page_list();
		$data1 = mysql_fetch_array($query1);
		return $data1;
	}
	
	function page_footer($uri = null)
	{
		$linkPage = $_SERVER['REDIRECT_URL'];
		$linkPage = preg_replace('/\/page[0-9]+$/is', '', $linkPage);
		if ($this -> pages > 1 )
		{		
			$starter = 1;
			$stoper = 10;
			
			if ($this -> page > 6)
			{
				$starter = $this -> page - 5;
				$stoper = $this -> page + 5;
				echo '<a class="numbers" href="'. HOME . $linkPage .'/page1'. $uri .'">В начало</a> ... ';
			}
			
			for ($i = $starter; $i < $stoper; $i++)
			{
				if ($i == $this -> page)
				{
					echo '<span class="numbers_selected">'. $this -> page .'</span> ';
				}
				else
				{
					echo '<a class="numbers" href="'. HOME . $linkPage .'/page' . $i .  $uri .'">'. $i.'</a> ';
				}
				
				if ($i == $this -> pages)
					break;
			}
			
			if ( $this -> pages > $stoper )
				echo ' ... <a class="numbers" href="/page'. $this -> pages . $uri .'">В конец</a> ';
			
		}
	}
	
	function get_count($tableInput = null, $queryString = null)
	{
		$query1 = "SELECT COUNT(*) FROM `". $this -> DB_PREFIX . $tableInput ."` ". $queryString .";";
		
		$sql1 = $this -> sql($query1);
		$data1 = mysql_fetch_array($sql1);
		return $data1['COUNT(*)'];
	}
	
	function delete($tableInput = null, $queryString = null)
	{
		$query = "DELETE FROM `". $this -> DB_PREFIX . $tableInput ."` ". $queryString .";";
		$this -> sql($query);
	}
	
	function getRss($tableInput)
	{
		$this -> tableName = $this -> DB_PREFIX . $tableInput;
		
		$query = "SELECT * FROM `". $this -> tableName ."` ORDER BY `id` DESC LIMIT 15";
		return $this -> sql($query);
	}
	
	function getSitemapNewsUrls($tableInput, $column)
	{
		$this -> tableName = $this -> DB_PREFIX . $tableInput;
		
		$query = "SELECT `". $column ."` FROM `". $this -> tableName ."` ORDER BY `id` DESC LIMIT 9999";
		return $this -> sql($query);
	}
}

?>